package com.xiaoyu.awakening.deepdoc.plugin.parse;

import cn.hutool.core.io.IoUtil;
import com.xiaoyu.awakening.deepdoc.enums.FileFormatEnums;
import com.xiaoyu.awakening.deepdoc.plugin.AbstractAwkPlugin;
import com.xiaoyu.awakening.deepdoc.plugin.AwkParsePlugin;
import jakarta.annotation.Resource;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * TikaParsePlugin
 *
 * @author wangxiaoyu
 * @date 2025/3/14 17:40
 * @since v1.0
 */
@Component
public class TikaParsePlugin extends AbstractAwkPlugin implements AwkParsePlugin {

    @Resource
    private Tika tika;

    @Override
    protected void initAndConfig() {
    }

    @Override
    public FileFormatEnums getFileFormat(byte[] file) {
        String detectFormat = tika.detect(file);
        return transTikaFormat2Common(detectFormat);
    }

    @Override
    public String file2text(byte[] file, FileFormatEnums format) {
        ByteArrayInputStream stream = IoUtil.toStream(file);
        try {
            return tika.parseToString(stream);
        } catch (IOException | TikaException e) {
            throw new RuntimeException(e);
        }
    }

    private FileFormatEnums transTikaFormat2Common(String tikaFormat) {
        return null;
    }
}
